/*
 * @Description: 
 * @Author: xuqi
 * @Github: https://github.com/knight-peter
 * @LastEditors: xuqi
 */
/**

 @Name：用户管理
 @Author：xuqipeter
 @Site：http://blog.xuziyu.cn/
    
 */
layui.extend({
  citypicker: '../assets/plugins/city-picker/city-picker'
}).define(function (exports) {
  layui.use(['citypicker', 'table', 'layer', 'm-utils', 'form', 'admin', 'view', 'OrderModel'], function () {
    var $ = layui.$,
      table = layui.table,
      layer = layui.layer,
      admin = layui.admin,
      view = layui.view,
      form = layui.form,
      setter = layui.setter,
      baseUrl = setter.baseUrl,
      utils = layui['m-utils'],
      cityPicker = layui.citypicker,
      orderModel = layui.OrderModel;

    form.render();
    orderModel.tableList = orderModel._tableList()
    /* search重载table */
    form.on('submit(js-btn-table-search)', function (data) {
      let field = data.field;
      orderModel.tableList.reload({
        where: field
      });
      return false;
    });
    table.on('toolbar(tableList)', function (obj) {
      switch (obj.event) {
        case 'add':
          _layerAdd();
          layer.full(orderModel.layerFull);
          break;
      }
    });
    /*监听主页面table行工具栏*/
    var tableObj;
    table.on('tool(tableList)', function (obj) {
      tableObj = obj;
      const data = obj.data;
      const layEvent = obj.event;
      const tr = obj.tr;
      switch (layEvent) {
        case 'detail':
          _layerDetail(tableObj);
          layer.full(orderModel.layerFull);
          break;
        case 'edit':
          _layerEdit(tableObj);
          layer.full(orderModel.layerFull);
          break;
        case 'send':
          _layerSend(tableObj);
          break;
        case 'del':
          _layerDel(tableObj);
      }
    });
    /* 增加 */
    function _layerAdd() {
      orderModel.layerFull = admin.popup({
        id: 'tableAdd',
        title: '新增新的订单',
        area: ['800px', '400px'],
        offset: 't',
        btn: ['提交', '取消'],
        btnAlign: 'c',
        maxmin: true,
        skin: 'myskin',
        yes: function (index, layero) {
          layer.confirm(`确定要新增吗？`, {
            icon: 3
          }, function (confirmIdx) {
            layer.close(confirmIdx);
            layero.find('[type="submit"]').trigger('click');
          })
        },
        success: function () {
          view(this.id).render('g-popup/order__add').done(function () {
            utils.tplRender('popupTplOrder', 'popupViewOrder', {}, function () {
              form.render();
              var currentPickerOrder = new cityPicker("#city-picker-order", {
                provincename: "provincenameid2",
                cityname: "citynameid2",
                districtname: "districtnameid2"
              });
              currentPickerOrder.setValue('');
            });
          })
        },
      });
    }
    /* 查看 */
    function _layerDetail(obj) {
      const data = obj.data
      orderModel.layerFull = admin.popup({
        id: 'detail',
        title: `订单编号：<b>${data.number}</b> 的详情信息`,
        area: ['800px', '400px'],
        offset: 't',
        btn: ['关闭'],
        btnAlign: 'c',
        maxmin: true,
        skin: 'myskin',
        success: function () {
          view(this.id).render('g-popup/user__detail').done(function () {
            utils.tplRender('popupTplOrder', 'popupViewOrder', data);
          })
        }
      });
    }
    /* 编辑 */
    function _layerEdit(obj) {
      const data = obj.data
      orderModel.layerFull = admin.popup({
        id: 'tableAdd',
        title: `订单编号：<b>${data.number}</b> 的详情信息`,
        area: ['800px', '400px'],
        offset: 't',
        btn: ['提交', '取消'],
        btnAlign: 'c',
        maxmin: true,
        skin: 'myskin',
        yes: function (index, layero) {
          layer.confirm(`确定要编辑 订单编号：<b>${data.number}</b> 吗？`, {
            icon: 3
          }, function (confirmIdx) {
            layer.close(confirmIdx);
            layero.find('[type="submit"]').trigger('click');
          })
        },
        success: function () {
          view(this.id).render('g-popup/order__add').done(function () {
            utils.tplRender('popupTplOrder', 'popupViewOrder', data, function () {
              form.render();
            });
          })
        },
      });
    }
    /* 发货 */
    function _layerSend(obj) {
      const data = obj.data
      orderModel.layerFull = admin.popup({
        id: 'tableAdd',
        title: `订单编号：<b>${data.order_sn}</b> 的发货信息`,
        area: ['800px', '400px'],
        offset: 't',
        btn: ['提交', '取消'],
        btnAlign: 'c',
        maxmin: true,
        skin: 'myskin',
        yes: function (index, layero) {
          layer.confirm(`确定要发货 订单编号：<b>${data.order_sn}</b> 吗？`, {
            icon: 3
          }, function (confirmIdx) {
            layer.close(confirmIdx);
            layero.find('[type="submit"]').trigger('click');
          })
        },
        success: function () {
          view(this.id).render('g-popup/order__send').done(function () {
            utils.tplRender('popupTplSend', 'popupViewSend', data, function () {
              form.render();
            });
          })
        },
      });
    }
    /* 删除 */
    function _layerDel(obj) {
      const data = obj.data;
      const tr = obj.tr;
      // console.log(obj)
      layer.confirm(`确定要删除 订单编号：<b>${data.number}</b> 吗？`, {
        icon: 0
      }, function (confirmIdx) {
        layer.close(confirmIdx);
        tr.remove();
        layer.msg('删除成功', {
          icon: 1,
          itme: 1500
        })
      })
    }
    /* 提交发货表单 */
    form.on('submit(submitBtnSend)', function (res) {
      let fieldData = utils.formDataFilter(res.field) || '';
      orderModel._deliverOrder({
        fieldData
      })
      return false;
    })
    /* 提交新增订单 */
    form.on('submit(submitBtnAdd)', function (res) {
      let fieldData = utils.formDataFilter(res.field) || '';
      orderModel._createOrder({
        fieldData
      })
      return false;
    })
  })

  exports('m-order', {})
})